package com.copa.l2325;

import java.util.HashMap;
import java.util.Map;

/**
 * @author copa
 * @createDate 2023-02-01 09:08
 * @function 哈希表法
 */
public class Test1 {

    public String decodeMessage(String key, String message) {
        Map<Character, Character> map = new HashMap<>();
        key = key.replace(" ", "");
        char[] keyArray = key.toCharArray();
        // 构建哈希表，形成对照
        for (char c : keyArray) {
            if (map.get(c) == null) {
                // 如果该字母首次出现则写入
                map.put(c, (char)(map.size() + 'a'));
            }
            if (map.size() >= 26) {
                // 如果map数量达到26个无需遍历
                break;
            }
        }
        // 开始解密
        StringBuilder sb = new StringBuilder();
        char[] messageArray = message.toCharArray();
        for (char c : messageArray) {
            if (c != ' ') {
                sb.append(map.get(c));
                continue;
            }
            sb.append(c);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String key = "the quick brown fox jumps over the lazy dog";
        String message = "vkbs bs t suepuv";
        System.out.println(new Test1().decodeMessage(key, message));
    }
}
